#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re


def find_pattern_from_pi(pattern, pi):
    """从pi中找到符合规则的"""

    match_result = re.finditer(pattern, pi)
    result = [(res.start(), res.group()) for res in match_result]
    # all_index = []
    # for i in match_result:
    #     all_index.append(i.span()[0])
    print(f"年份1960--2021在pi的所有位置: {result}, 共{len(result)}个")


if __name__ == '__main__':
    with open("pi.txt", 'r') as fp:
        pi = fp.read()
    #
    # # 题一 1980-2000
    """题一放弃"""
    pattern1 = r'(19[8-9]\d|2000)'
    find_pattern_from_pi(pattern1, pi)

    # # 题二
    # # """输出年份 1960--2021 的 正则规则，并在 pi 中找出所有符合规则的"""
    pattern2 = r'(19[6-9]\d|20[0-1]\d|202[0-1])'
    find_pattern_from_pi(pattern2, pi)
